package com.hyk.job;


import com.alipay.api.AlipayApiException;
import com.hyk.service.IUserOrderService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Component
public class OrderJob {

    public static final String CLEARORDER = "clearOrder";

    @Resource
    private IUserOrderService orderService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /*
    *清理15分钟还未支付的订单*/
    @Scheduled(cron = "0 0/1 * * * ? ")
    private void cleanOrder() throws AlipayApiException {
        System.out.println("执行了定时任务");
        Object clearOrder = redisTemplate.opsForValue().get(CLEARORDER);
        if (clearOrder != null){
            return;
        }
        redisTemplate.opsForValue().set(CLEARORDER, UUID.randomUUID().toString(),6, TimeUnit.MINUTES);
        orderService.clearOrder();
    }

}
